﻿using MvvmFoundation.Wpf;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace ServiceOperater
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : CatchUnHandExceptionApp
    {


    }

    public partial class CatchUnHandExceptionApp : Application
    {
        public static readonly Messenger Messenger;

        static CatchUnHandExceptionApp()
        {
            Messenger = new Messenger();
        }



        public CatchUnHandExceptionApp()
        {
            TestOracleConnectCondition();
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        private void TestOracleConnectCondition()
        {
            this.Startup += App_Startup;
            //this.Exit += App_Exit;
            //this.LoadCompleted += App_LoadCompleted;
        }

        private void App_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
           
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {

        }

        private void App_Startup(object sender, StartupEventArgs e)
        {
            

        }


        #region 全局异常捕获
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                if (e.ExceptionObject is System.Exception)
                {
                    HandleException((System.Exception)e.ExceptionObject);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                HandleException(e.Exception);
                e.Handled = true;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        public static void HandleException(Exception ex)
        {
            //记录日志
            if (!System.IO.Directory.Exists("Log"))
            {
                System.IO.Directory.CreateDirectory("Log");
            }
            DateTime now = DateTime.Now;
            string logpath = string.Format(@"Log\fatal_{0}{1}{2}.log", now.Year, now.Month, now.Day);
            System.IO.File.AppendAllText(logpath, string.Format("\r\n----------------------{0}--------------------------\r\n", now.ToString("yyyy-MM-dd HH:mm:ss")));
            System.IO.File.AppendAllText(logpath, ex.Message);
            if (ex.InnerException != null)
            {
                System.IO.File.AppendAllText(logpath, "\r\n");
                System.IO.File.AppendAllText(logpath, ex.InnerException.Message);
            }
            System.IO.File.AppendAllText(logpath, "\r\n");
            System.IO.File.AppendAllText(logpath, ex.StackTrace);
            System.IO.File.AppendAllText(logpath, "\r\n");
            System.IO.File.AppendAllText(logpath, "\r\n----------------------footer--------------------------\r\n");

            MessageBox.Show("发生未知错误!请重新查询.或将日志发送给程序员.");
        }
        #endregion
    }
}
